/*
 * Copyright 2012 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.util;

import java.util.Iterator;

/**
 * An iterator over an array.
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 *
 * @param <T> The type of the elements in the array.
 */
public class ArrayIterator<T> implements Iterator<T>
{

	/*
	 * The array to iterate over.
	 */
	private T[] values;
	
	/*
	 * The position of the second last element in the array of the iteration
	 * (<b>excluding</b> the element on position <code>end</code>).
	 */
	private int end;
	
	/*
	 * A counter variable to store the state of the iteration.
	 */
	private int pos;
	
	/**
	 * Constructor method.
	 * 
	 * The iterator returns all elements in <code>values</code> starting at position
	 * <code>start</code> and stopping on the element on position <code>end - 1</code>.
	 * 
	 * @param values The array to iterate over.
	 * @param start The position where the iteration starts (<b>including</b> element on
	 * position <code>start</code>).
	 * @param end The position of the second last element in the array of the iteration
	 * (<b>excluding</b> the element on position <code>end</code>).
	 */
	public ArrayIterator(T[] values, int start, int end)
	{
		this.values = values;
		this.end = end;
		this.pos = start;
	}
	
	/**
	 * The same as {@link #ArrayIterator(Object[], int, int)} but uses as <code>end</code>
	 * value the length of the array, so the iteration contains all elements from position
	 * <code>start</code> to the last element.
	 * 
	 * @param values The array to iterate over.
	 * @param start The position where the iteration starts (<b>including</b> element on
	 * position <code>start</code>).
	 * 
	 * @see #ArrayIterator(Object[], int, int)
	 */
	public ArrayIterator(T[] values, int start)
	{
		this(values, start, values.length);
	}
	
	public ArrayIterator(T[] values)
	{
		this(values, 0);
	}
	
	@Override
	public boolean hasNext()
	{
		return pos < end;
	}

	@Override
	public T next()
	{
		return values[pos++];
	}

	@Override
	public void remove()
	{
		throw new UnsupportedOperationException("The remove method is not supported by the ArrayIterator.");
	}

}
